﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using hn.teacher.DBoperation;
using hn.teacher.wdclass;
using System.Text.RegularExpressions;

public partial class Register : System.Web.UI.Page
{
    EDBoperation edb = new EDBoperation();
    RDBoperation rdb = new RDBoperation();
    BDBoperation bdb = new BDBoperation();
    public int userID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind();
        }
    }
    protected void Bind()
    {
        Bind_ddl_Forg(ddl_Forg);
        Bind_ddl_Sorg(ddl_Sorg);
    }

    public void Bind_ddl_Forg(DropDownList ddl)
    {
        string procName = "usp_SelectT_PUB_FIRST_ORGsAll";
        DataTable dt_First = rdb.RunProcQuery(procName, null);
        ddl.DataSource = dt_First;
        ddl.DataTextField = "FORG_NAME";
        ddl.DataValueField = "FORG_ID";
        ddl.DataBind();
        ddl.Items.Insert(0, "");
    }

    public void Bind_ddl_Sorg(DropDownList ddl)
    {
        string firstOrgID = "";
        firstOrgID = ddl.SelectedValue.ToString();
        SqlParameter[] sp1 = new SqlParameter[] { new SqlParameter("@FORG_ID", firstOrgID) };
        string procName1 = "usp_SelectT_PUB_SECOND_ORG_BY_FIRST_ORG";
        DataTable dt_Second = rdb.RunProcQuery(procName1, sp1); ;
        ddl.DataSource = dt_Second;
        ddl.DataTextField = "SORG_NAME";
        ddl.DataValueField = "SORG_ID";
        ddl.DataBind();
        ddl.Items.Insert(0, "");
    }
    protected void bnt_back_Click(object sender, EventArgs e)
    {
        Response.Redirect("login.aspx");
    }
    protected void btnTest_Click(object sender, EventArgs e)
    {
        if (txt_LoginName.Text.Trim() == "")
        {
            this.lbl_name.Text = "*请填写用户名！";
            txt_LoginName.Focus();
        }
        else
        {
            SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@USER_NAME", txt_LoginName.Text.Trim()) };
            string procName = "usp_SelectT_PUB_USER_BY_NAME";
            DataTable dt = bdb.RunProcQuery(procName, sp);
            if (dt.Rows.Count > 0 && txt_LoginName.Text.Trim() != "")
            {
                this.lbl_name.Text = "*用户名已存在！";
                txt_LoginName.Focus();
            }
            else 
            {
                this.lbl_name.Text = "";
            }
        }
    }
    protected void txt_Pwd_TextChanged(object sender, EventArgs e)
    {
        if (txt_Pwd.Text.ToString().Trim() == ""||txt_Pwd.Text.ToString().Trim() ==null)
        {
            this.lbl_pwd.Text = "*密码不能为空！";
            txt_Pwd.Focus();
        }
    }

    protected void btnTestID_Click(object sender, EventArgs e)
    {
        Match m = Regex.Match(this.txt_userid.Text, "^[0-9]{3,13}$");
        if (!m.Success)
        {
            this.lbl_id.Text = "*用户编号必须为数字！";
            txt_userid.Focus();
        }
        if (txt_userid.Text.Trim() == "" || txt_userid.Text.Trim()==null)
        {
            this.lbl_id.Text = "*请填写编号！";
            txt_userid.Focus();
        }
        else if (m.Success)
        {
            SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@USER_ID", int.Parse(txt_userid.Text.Trim())) };
            string procName = "usp_SelectT_PUB_USER_BY_ID";
            DataTable dt = bdb.RunProcQuery(procName, sp);
            if (dt.Rows.Count > 0 && txt_userid.Text.Trim() != "")
            {
                this.lbl_id.Text = "*编号已存在！";
                txt_userid.Focus();
            }
            else 
            {
                this.lbl_id.Text = "";
            }
        }
    }
    protected void ddlforg_SelectedIndexChanged(object sender, EventArgs e)
    {
        string firstOrgID = "";
        firstOrgID = ddl_Forg.SelectedValue.ToString();
        SqlParameter[] sp1 = new SqlParameter[] { new SqlParameter("@FORG_ID", firstOrgID) };
        string procName1 = "usp_SelectT_PUB_SECOND_ORG_BY_FIRST_ORG";
        DataTable dt_Second = rdb.RunProcQuery(procName1, sp1);
        ddl_Sorg.DataSource = dt_Second;
        ddl_Sorg.DataTextField = "SORG_NAME";
        ddl_Sorg.DataValueField = "SORG_ID";
        ddl_Sorg.DataBind();
        Bind_ddl_Sorg(ddl_Sorg);
    }
    //注册
    protected void btnRegister_Click(object sender, EventArgs e)
    {

        int userid = int.Parse(txt_userid.Text.ToString().Trim());
        string userName = txt_LoginName.Text.Trim();
        string password = txt_Pwd.Text.Trim();
        string trueName = txt_TName.Text.Trim();
        bool flagSex = true;
        if (rb_Man.Checked == true)
        {
            flagSex = true;
        }
        else if (rb_Women.Checked == true)
        {
            flagSex = false;
        }
        string cellPhone = txt_CelPhone.Text.Trim();
        string tel = txt_Tel.Text.Trim();
        string address = txt_Address.Text.Trim();
        string qq = txt_QQ.Text.Trim();
        string email = txt_Email.Text.Trim();
        string forg = ddl_Forg.SelectedValue.ToString();
        string sorg = ddl_Sorg.SelectedValue.ToString();
        string districtID = "";
        if (sorg == "")
        {
            districtID = forg;
        }
        else
        {
            districtID = sorg;
        }
        bool flagValid = true;
        bool flagMember = true;
        if (txt_LoginName.Enabled == true && txt_LoginName.ReadOnly == false)
        {
            SqlParameter[] sp1 = new SqlParameter[] { new SqlParameter("@USER_NAME",userName),new SqlParameter("USER_ID",userID) };
            string procName1 = "usp_SelectT_PUB_USER_BY_USERNAME_OR_USERID";
            DataTable dt1 = bdb.RunProcQuery(procName1, sp1);
            if (dt1.Rows.Count > 0)
            {
                Response.Write("<Script>alert('用户名或用户编号已存在！')</Script>");
                return;
            }
            if (txt_LoginName.Text.ToString().Trim().ToLower().IndexOf("user") == 0)
            {
                Response.Write("<Script>alert('用户名不能以user开头！')</Script>");
                return;
            }


        }
        //插入用户表
        if (userid != 0 && userid != null || userName != "")
        {
            SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@USER_ID",userid), 
                new SqlParameter("@DISTRICT_ID", districtID),
                new SqlParameter("@USER_NAME", userName),new SqlParameter("@PASSWORD", password),
                new SqlParameter("@NAME", trueName),new SqlParameter("@SEX", flagSex),
                new SqlParameter("@CELPHONE", cellPhone),new SqlParameter("@PHONE", tel),
                new SqlParameter("@ADDRESS", address),new SqlParameter("@QQ", qq),
                new SqlParameter("@EMAIL", email),new SqlParameter("@FORG_ID", forg),
                new SqlParameter("@SORG_ID", sorg),new SqlParameter("@IS_ACTIVATED", flagValid),
                new SqlParameter("@IS_MEMBER", flagMember)};
            string procName = "usp_Insert_PUB_USER";
            bool flag1 = bdb.RunProcNonQuery(procName, sp);

            //插入教师表
            SqlParameter[] sp1 = new SqlParameter[] { new SqlParameter("@USER_ID", userid) };
            string procName1 = "usp_InsertT_PUB_teacher";
            bool flag2 = bdb.RunProcNonQuery(procName1, sp1);

            //插入用户角色表
            SqlParameter[] sp2 = new SqlParameter[] {new SqlParameter("@USER_ID",userid),
        new SqlParameter("ROLE_ID",2),new SqlParameter("@ORG_ID",districtID)};
            string procName2 = "usp_InsertT_USER_ROLE";
            bool flag3 = bdb.RunProcNonQuery(procName2, sp2);
            if (flag1 == true && flag2 == true && flag3 == true)
            {
                Response.Write("<Script>alert('注册成功！')</Script>");
            }
        }
        else
        {
            Response.Write("<Script>alert('注册失败！')</Script>");
        }
    }

}